package day_2022_9_to_12.Day;

/**
 * @author haomin
 * @date 2022/09/14 20:07
 **/
public class Day12_3_Num337_Rob {
    class Solution {
        public int rob(TreeNode root) {
            int[] res = dfs(root);
            return Math.max(res[0], res[1]);
        }
        private int[] dfs(TreeNode root){
            if(root == null) return new int[]{0, 0};
            int[] l = dfs(root.left);
            int[] r = dfs(root.right);
            // 偷当前节点，就不能偷子节点
            int select = root.val + l[1] + r[1];
            // 不偷当前节点，就可以选择偷或者不偷子节点，选择值大的就行
            int notSelect = Math.max(l[0], l[1]) + Math.max(r[0], r[1]);
            return new int[]{select, notSelect};
        }
    }
}